package de.yellowshoes.tedx.persistence;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import de.yellowshoes.tedx.TEDxConstants;
import de.yellowshoes.tedx.persistence.entity.Agenda;
import de.yellowshoes.tedx.persistence.entity.Eventdetail;
import de.yellowshoes.tedx.persistence.entity.Organizer;
import de.yellowshoes.tedx.persistence.entity.Partner;
import de.yellowshoes.tedx.persistence.entity.Speaker;

public class PersistenceHelper extends OrmLiteSqliteOpenHelper
    {

    private static final String                         DATABASE_NAME    = "tedx.db";
    private static final Integer                        DATABASE_VERSION = 1;

    private RuntimeExceptionDao< Speaker, String >      speakerDao       = null;
    private RuntimeExceptionDao< Partner, String >      partnerDao       = null;
    private RuntimeExceptionDao< Organizer, String >    organizerDao     = null;
    private RuntimeExceptionDao< Agenda, String >       agendaDao        = null;
    private RuntimeExceptionDao< Eventdetail, Integer > eventdetailDao   = null;

    public PersistenceHelper( Context context )
        {
        super( context, DATABASE_NAME, null, DATABASE_VERSION );
        }

    @Override
    public void onCreate( SQLiteDatabase db, ConnectionSource conn )
        {
        try
            {
            TableUtils.createTable( conn, Speaker.class );
            TableUtils.createTable( conn, Partner.class );
            TableUtils.createTable( conn, Organizer.class );
            TableUtils.createTable( conn, Agenda.class );
            TableUtils.createTable( conn, Eventdetail.class );
            }
        catch( SQLException e )
            {
            Log.e( TEDxConstants.LOG_TAG, "Can't create database", e );
            throw new RuntimeException( e );
            }
        }

    @Override
    public void onUpgrade( SQLiteDatabase db, ConnectionSource conn, int oldVersion, int newVersion )
        {
        try
            {
            TableUtils.dropTable( conn, Speaker.class, true );
            TableUtils.dropTable( conn, Partner.class, true );
            TableUtils.dropTable( conn, Organizer.class, true );
            TableUtils.dropTable( conn, Agenda.class, true );
            TableUtils.dropTable( conn, Eventdetail.class, true );
            }
        catch( SQLException e )
            {
            Log.e( TEDxConstants.LOG_TAG, "Can't drop database", e );
            throw new RuntimeException( e );
            }
        }

    public RuntimeExceptionDao< Speaker, String > getSpeakerDao()
        {
        if( speakerDao == null )
            {
            speakerDao = getRuntimeExceptionDao( Speaker.class );
            }

        return speakerDao;
        }

    public RuntimeExceptionDao< Partner, String > getPartnerDao()
        {
        if( partnerDao == null )
            {
            partnerDao = getRuntimeExceptionDao( Partner.class );
            }

        return partnerDao;
        }

    public RuntimeExceptionDao< Organizer, String > getOrganizerDao()
        {
        if( organizerDao == null )
            {
            organizerDao = getRuntimeExceptionDao( Organizer.class );
            }

        return organizerDao;
        }

    public RuntimeExceptionDao< Agenda, String > getAgendaDao()
        {
        if( agendaDao == null )
            {
            agendaDao = getRuntimeExceptionDao( Agenda.class );
            }

        return agendaDao;
        }

    public RuntimeExceptionDao< Eventdetail, Integer > getEventdetailDao()
        {
        if( eventdetailDao == null )
            {
            eventdetailDao = getRuntimeExceptionDao( Eventdetail.class );
            }

        return eventdetailDao;
        }

    @Override
    public void close()
        {
        super.close();
        partnerDao = null;
        speakerDao = null;
        organizerDao = null;
        agendaDao = null;
        eventdetailDao = null;
        }
    }
